package tang;

import com.google.common.collect.Lists;
import com.suxin.domain.Good;
import com.suxin.domain.UserInfo;
import com.suxin.enums.BaseEnum;
import com.suxin.utils.ClassUtil;
import org.apache.ibatis.datasource.pooled.PooledDataSourceFactory;
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.parsing.XPathParser;
import org.apache.ibatis.scripting.xmltags.ExpressionEvaluator;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileInputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Objects;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @author Tang
 * @classname NoContextMain
 * @description [ https://blog.csdn.net/crave_shy/article/details/46611205 ]
 * @date 2021/5/6 20:00
 */
public class NoContextMain {

    @Test
    public void test08() throws Exception{
        final XPathParser xPathParser = new XPathParser(new FileInputStream("/Users/tanglonglong/IdeaProjects/cloud-test/web-springboot/web-mybatis/src/main/resources/mapper/Goods0Mapper.xml"));
        final XNode rootNode = xPathParser.evalNode("/mapper/select");
        System.out.println("--->" + rootNode.getStringBody());
        for (XNode xNode : rootNode.getChildren()) {
            System.out.println("--->" + xNode);
        }
    }


    public static void main(String[] args) throws Exception{
        final Good good = new Good();
        good.setGoodsName("12");
        final ExpressionEvaluator evaluator = new ExpressionEvaluator();
        System.out.println(evaluator.evaluateBoolean("goodsName != null and goodsName != ''", good));
    }


    @Test
    public void test01(){
        ClassUtil.getAllAssignedClass(BaseEnum.class).forEach(System.out::println);
    }

    @Test
    public void test02() throws Exception {
        JarURLConnection jarURLConnection = null;
        URL url = new URL("jar:file:G:\\javaConfig\\alibaba\\seata-1.3.0\\cloud-test\\web-springboot\\web-mybatis\\target\\web-mybatis-1.0-SNAPSHOT.jar!/");
        jarURLConnection = (JarURLConnection) url.openConnection();
        JarFile jarFile = jarURLConnection.getJarFile();
        Enumeration<JarEntry> entries = jarFile.entries();
//        String pack = BaseEnum.class.getPackage().getName().replace(".", File.separatorChar + "");
        String pack = BaseEnum.class.getPackage().getName().replace(".", "/");
//        System.out.println(pack);
        List<String> path = new ArrayList<>(10);
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            String pathClassName = jarEntry.getName();
            if (pathClassName.startsWith(pack)) {
//                path.add(pathClassName.replace(File.pathSeparator, ".").substring(0, pathClassName.length() - 6));
                String file = pathClassName.replace("/", ".").substring(0, pathClassName.length() - 6);
                if (!file.endsWith(".")) {
                    path.add(file);
                }
            }
        }
        path.forEach(System.out::println);
    }


    @Test
    public void test03(){
        PooledDataSourceFactory dataSourceFactory = new PooledDataSourceFactory();
        dataSourceFactory.setProperties(null);
        DataSource dataSource = dataSourceFactory.getDataSource();
        try {
            System.out.println(dataSource.getConnection());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Test
    public void test07(){
        for (int i = 0; i < 7; i++) {
            System.out.println(i);
            break;
        }
    }

    @Test
    public void test09(){
        String s1 = "abc";
        String s2 = "abc";

        String s3 = new String("abc");
        System.out.println(s1 == s2);
        System.out.println(s1 == s3);

        final ArrayList<? extends UserInfo> arrayList = Lists.newArrayList();
    }


}